require(['./config'],() => {
    require(['template','request','header','elevatezoom','footer','jquery'],(template,req,header,elevatezoom) => {
        class Deatils{
            constructor(){
                this.count = 1;
                this.getData()
            }
                getData(){
                    //获取地址栏id
                    const id = window.location.search.split('=')[1]
                    // console.log(id)
                    req.getDetailsData(id)
                    .then(res => {
                        // console.log(res)
                        if(res.code === 200){
                            this.render(res.data.detail)
                        }
                    })
                }
                //渲染页面
                render(res){
                    // console.log(res)
                    const {image,id,photo,saleNum,title,descContentList,price} = res
                    const html = template('detailsTemplate',{image,id,photo,saleNum,title,descContentList,price})
                    $('.box').html(html)
                    // console.log(descContentList)
                    $('.product').html(template('productTemplate',{descContentList}))
                    // this.initZoom()
                    this.addReduce()
                    this.addToCar(id,image,title,price)
                    this.initZoom()
                }
                //放大镜
                initZoom(){
                    $('.zoom-image').elevateZoom()
                }
                //数量加减
                addReduce(){
                    $('.reduceBtn').click(()=>{
                        this.count --
                        if(this.count <= 1){
                            this.count =1
                        }
                        $('.countBox').val(this.count)
                    })
                    $('.addBtn').click(()=>{
                        this.count ++
                        $('.countBox').val(this.count)
                    })
                }
                //点击加入购物车 localStorage
                addToCar(id,image,title,price){
                 
                    $('.addToCar').click(() => {
                        let prodList = localStorage.getItem('prodList')
                        // console.log(prodList)
                        if(prodList){
                            prodList = JSON.parse(prodList)
                           
                            const flag = prodList.some((item,index)=>{
                                return item.id === id
                            })
                            if(flag){
                                prodList.forEach(item =>{
                                    item.count += item.id === id ? this.count : 0
                                })
                            }else{
                                prodList.push({id,image,title,price,count:this.count,isChecked:true})
                            }
                            // console.log(prodList)
                            localStorage.setItem('prodList',JSON.stringify(prodList))
                        }else{
                            localStorage.setItem('prodList',JSON.stringify([{id,image,title,price,count:this.count,isChecked:true}]))
                        }
                        $('.loading').fadeIn(2000,()=>{
                            $('.loading').fadeOut(2000)
                        })
                        header.computedCount()
                       
                    })
                    
                }

        }
        return new Deatils()
    })
})
    
    

